<template>
  <div class="add-depa">
    <el-dialog title="编辑部门" :visible="showDialog">
      <el-form
        label-width="100px"
        :model="formData"
        :rules="rules"
        ref="myformData"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="部门编号" prop="code">
          <el-input v-model="formData.code"></el-input>
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select
            v-model="formData.manager"
            type="selecter"
            placeholder="请选择"
          >
            <el-option
              v-for="item in simple"
              :value="item.username"
              :key="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="formData.introduce"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addDepartments } from '../../../api/departments'
import { getSimpleApi } from '../../../api/simple'
export default {
  props: ['showDialog', 'curren', 'allList'],
  data () {
    return {
      formData: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称1-50个字', trigger: 'blur' },
          {
            validator: (rules, value, callback) => {
              if (this.formData.id) {
                var bool = this.allList
                  .filter(item => {
                    return item.id === this.curren.id && item.name !== value
                  })
                  .some(item => {
                    return item.name === value
                  })
              } else {
                var bool = this.allList
                  .filter(item => {
                    return item.pid === this.curren.id
                  })
                  .some(item => {
                    return item.name === value
                  })
              }
              bool
                ? callback(new Error('当前部门下已有同名的部门'))
                : callback()
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称1-50个字', trigger: 'blur' },
          {
            validator: (rules, value, callback) => {
              if (this.formData.id) {
                var bool = this.allList.some(item => {
                  return item.id === this.curren.id && item.code === value
                })
              } else {
                var bool = this.allList.some(item => {
                  return item.code === value
                })
              }
              bool ? callback(new Error('当前code与其他部门冲突')) : callback()
            },
            trigger: 'blur'
          }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门名称1-50个字', trigger: 'blur' }
        ]
      },
      simple: []
    }
  },
  created () {
    this.showSelect()
  },
  methods: {
    async showSelect () {
      const res = await getSimpleApi()
      this.simple = res
    },
    submit () {
      this.$refs.myformData.validate(async valid => {
        if (valid) {
          const res = await addDepartments({
            ...this.formData,
            pid: this.curren.id
          })
          this.$message({
            type: 'info',
            message: '新增成功'
          })
          this.$emit('update:showDialog', false)
          this.$emit('refreshdata')
        } else {
          return
        }
      })
    },
    cancel () {
      this.$emit('update:showDialog', false)
      this.formData = {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      }
      this.$refs.myformData.resetFields()
    },
    editDepartments (data) {
      this.formData = data
    }
  }
}
</script>

<style>
.el-dialog__footer {
  display: flex;
  justify-content: center;
}
</style>
